import Loading from './Loading'
import Vue from 'vue';
let vLoadingCp = Vue.extend(Loading)
let isXX = true
const defaults = {
  text: null,
  fullscreen: true,
  body: false,
  lock: false,
  customClass: ''
};
let fullscreenLoading;
const LoadingConstructor = Vue.extend(vLoadingCp);
vLoadingCp.prototype.close = function (){
  fullscreenLoading = undefined;
  this.isOk = true;
  setTimeout(()=>{
    this.isShow = false
    if(this.$el.parentNode){
      this.$el.parentNode.removeChild(this.$el);
    }
    this.$destroy();

  },300);

}

const vLoading = ()=>{
  if( fullscreenLoading ){
    return fullscreenLoading
  }

  let parent = document.querySelector('.nav-box')||document.querySelector('body')
  parent.style.position = 'relative';
  //
  let _el = document.createElement('div');
  // _el.className = 'pouiuiou'
  let instance =  fullscreenLoading = new vLoadingCp({
    el:_el,
    // data: options
  });
  instance.isShow = true;
  parent.appendChild(instance.$el)
  return instance

}

function afterLeave(instance, callback, speed = 300, once = false){
    if (!instance || !callback) throw new Error('instance & callback is required');
    let called = false;
    const afterLeaveCallback = function() {
      if (called) return;
      called = true;
      if (callback) {
        callback.apply(null, arguments);
      }
    };
    if (once) {
      instance.$once('after-leave', afterLeaveCallback);
    } else {
      instance.$on('after-leave', afterLeaveCallback);
    }
    setTimeout(() => {
      afterLeaveCallback();
    }, speed + 100);
  };


export default  vLoading